/**
 * 
 */
package pt.mleiria.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import pt.mleiria.vo.ContactsVO;
import pt.mleiria.vo.PricesVO;

/**
 * @author root
 *
 */
public class TextLoaderBOImpl implements TextLoaderBO, InitializingBean {
	private DataSource dataSource;
	
	/**
	 * @param dataSource the dataSource to set
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void afterPropertiesSet() throws Exception {
		if (dataSource == null)
			throw new BeanCreationException("A datasource é nula!");

	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#loadIntro()
	 */
	@Override
	public StringBuffer loadIntro(final String lang) {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		final String sql = "select mainText from menu_" + lang;
		final StringBuffer sb = new StringBuffer();
		jt.query(sql, new RowCallbackHandler() {
		     public void processRow(ResultSet rs) throws SQLException {
		         sb.append(rs.getString(1));
		       }
		     }
		  );
		return sb;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#loadGeography()
	 */
	@Override
	public StringBuffer loadGeography(final String lang) {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		final String sql = "select geography from menu_" + lang;
		final StringBuffer sb = new StringBuffer();
		jt.query(sql, new RowCallbackHandler() {
		     public void processRow(ResultSet rs) throws SQLException {
		         sb.append(rs.getString(1));
		       }
		     }
		  );
		return sb;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#loadHistory(java.lang.String)
	 */
	@Override
	public StringBuffer loadHistory(String lang) {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		final String sql = "select history from menu_" + lang;
		final StringBuffer sb = new StringBuffer();
		jt.query(sql, new RowCallbackHandler() {
		     public void processRow(ResultSet rs) throws SQLException {
		         sb.append(rs.getString(1));
		       }
		     }
		  );
		return sb;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#loadGastronomy(java.lang.String)
	 */
	@Override
	public StringBuffer loadGastronomy(String lang) {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		final String sql = "select gastronomy from menu_" + lang;
		final StringBuffer sb = new StringBuffer();
		jt.query(sql, new RowCallbackHandler() {
		     public void processRow(ResultSet rs) throws SQLException {
		         sb.append(rs.getString(1));
		       }
		     }
		  );
		return sb;
	}

	/* (non-Javadoc) 
	 * @see pt.mleiria.bo.TextLoaderBO#getContacts()
	 */
	@Override
	public ContactsVO getContacts() {
		ContactsVO contacts = null;
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		//Object[] parameters = new Object[] {new Integer(1)};
		SqlRowSet srs = jt.queryForRowSet("select * from contactos");
		while(srs.next()){
			contacts = new ContactsVO(srs.getString(2), srs.getString(3), srs.getString(4), srs.getString(5)); 
		}
		return contacts;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#getPricesDescription(java.lang.String)
	 */
	@Override
	public List<String> getPricesDescription(String lang) {
		List<PricesVO> prices = new ArrayList<PricesVO>();
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		//Object[] parameters = new Object[] {new Integer(1)};
		SqlRowSet srs = jt.queryForRowSet("select * from precos_" + lang);
		while(srs.next()){
			prices.add(new PricesVO(srs.getString(2), srs.getDouble(3), srs.getDouble(4)));
		}
		List<String> descriptions = new ArrayList<String>();
		for(PricesVO price : prices){
			if(lang.equals("pt_PT")){
				if(price.getPriceMin() == price.getPriceMax()){
					descriptions.add(price.getName() + ": €" + price.getPriceMin());
				}else{
					descriptions.add(price.getName() + ": de €" + price.getPriceMin() + " a €" + price.getPriceMax());
				}
			}else{
				if(price.getPriceMin() == price.getPriceMax()){
					descriptions.add(price.getName() + ": €" + price.getPriceMin());
				}else{
					descriptions.add(price.getName() + ": from €" + price.getPriceMin() + " to €" + price.getPriceMax());
				}
			}
			
		}
		return descriptions;
	}

	/* (non-Javadoc)
	 * @see pt.mleiria.bo.TextLoaderBO#loadTodo(java.lang.String)
	 */
	@Override
	public StringBuffer loadTodo(String lang) {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		final String sql = "select todo from menu_" + lang;
		final StringBuffer sb = new StringBuffer();
		jt.query(sql, new RowCallbackHandler() {
		     public void processRow(ResultSet rs) throws SQLException {
		         sb.append(rs.getString(1));
		       }
		     }
		  );
		return sb;
	}
	

}
